%% Author: 77
%% Created: 2011-7-6
%% Description: TODO: Add description to stop_vm_cmd
-module(stop_vm_cmd).
-date("2011.07.30").

%%
%% Include files
%%
-include("meta_schema.hrl").

%%
%% Exported Functions
%%
-export([handle/3]).

%%
%% API Functions
%%
handle(Content, From, State) ->
	io:format("handle info ~p ~p ~p ~p ~n",[?MODULE,Content,From,State]), %% debug info
	case check_parm(Content) of
		true ->
			io:format("check parameter pass~n"), %% debug info
			{VmId} = Content,
			io:format("request stop vmid:~p~n", [VmId]),
			case meta_data_ops:get_vm_runtime_info(VmId) of
				false ->
					gen_server:reply(From, {error,vm_not_exist}),
					false;
				Info ->
					Node = Info#vm_runtime_info.host_name,
					io:format("Node ~p ~n",[Node]), %% debug info
					case gen_server:call({node_server, list_to_atom(Node)}, {stop_vm, {VmId}}, 20000) of
						{error, Reason} ->
							gen_server:reply(From, {error, vm_stop_fail}),
							false;
						ok ->
							gen_server:reply(From, ok),
							ok
					end
			end;
		false ->
			gen_server:reply(From, {error, err_param}),
			false
	end.

%%
%% Local Functions
%%

check_parm(Content) ->
	true.
